package com.yds.developtools.views;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.widget.NestedScrollView;

import com.yds.developtools.R;

/**
 * 创建者：于德水
 * 创建时间：2021/12/9 0009 13:17
 * 创建描述：提示对话框
 */
@RequiresApi(api = Build.VERSION_CODES.M)
public class YDSAlertDialog extends Dialog implements View.OnClickListener, View.OnScrollChangeListener {
    private TextView mTextViewTitle;
    private NestedScrollView mNestedScrollViewMessage;
    private TextView mTextViewMessage;

    private View mViewSplitTitle;
    private View mViewSplitMessage;
    private View mViewSplitBottom;

    private TextView mButtonCancel;
    private TextView mButtonConfirm;


    private final float mWidthRatio;
    private final float mHeightRatio;

    private OnConfirmClickListener mOnConfirmClickListener;
    private OnCancelClickListener mOnCancelClickListener;
    private OnMessageScrollChangeListener mOnMessageScrollChangeListener;

    private boolean isScrollTop = true;
    private boolean isScrollBottom = false;

    /**
     * 有参构造
     *
     * @param context
     * @param widthRatio  宽度百分比 0-WRAP_CONTENT 1-MATCH_PARENT (0-1)-MATCH_PARENT*widthRatio
     * @param heightRatio 高度百分比 0-WRAP_CONTENT 1-MATCH_PARENT (0-1)-MATCH_PARENT*heightRatio
     */
    public YDSAlertDialog(@NonNull Context context, float widthRatio, float heightRatio) {
        this(context, widthRatio, heightRatio, R.style.normal_dialog);
    }

    public YDSAlertDialog(@NonNull Context context, float widthRatio, float heightRatio, int themeResId) {
        super(context, themeResId);
        this.mWidthRatio = widthRatio;
        this.mHeightRatio = heightRatio;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        @SuppressLint("InflateParams") View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_yds_alert, null);
        mTextViewTitle = view.findViewById(R.id.mTextViewTitle);
        mNestedScrollViewMessage = view.findViewById(R.id.mNestedScrollViewMessage);
        mNestedScrollViewMessage.setOnScrollChangeListener(this);
        mTextViewMessage = view.findViewById(R.id.mTextViewMessage);

        mViewSplitTitle = view.findViewById(R.id.mViewSplitTitle);
        mViewSplitMessage = view.findViewById(R.id.mViewSplitMessage);
        mViewSplitBottom = view.findViewById(R.id.mViewSplitBottom);

        mButtonCancel = view.findViewById(R.id.mButtonCancel);
        mButtonCancel.setOnClickListener(this);
        mButtonConfirm = view.findViewById(R.id.mButtonConfirm);
        mButtonConfirm.setOnClickListener(this);


        Window window = getWindow();
        window.getDecorView().setPadding(0, 20, 0, 20); //消除边距
        window.setWindowAnimations(R.style.AnimBottom);
        window.setGravity(Gravity.CENTER);
        DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
        window.setLayout(mWidthRatio == 0 ? ViewGroup.LayoutParams.WRAP_CONTENT : (int) (displayMetrics.widthPixels * mWidthRatio),
                mHeightRatio == 0 ? ViewGroup.LayoutParams.WRAP_CONTENT : (int) (displayMetrics.heightPixels * mHeightRatio));

        setContentView(view);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.mButtonCancel) {
            if (null != mOnCancelClickListener) {
                mOnCancelClickListener.onCancelClick();
            } else {
                dismiss();
            }
        } else if (v.getId() == R.id.mButtonConfirm) {
            if (null != mOnConfirmClickListener) {
                mOnConfirmClickListener.onConfirmClick();
            } else {
                dismiss();
            }
        }
    }

    @Override
    public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
        int messageHeight = mTextViewMessage.getHeight();
        int scrollViewMessageHeight = mNestedScrollViewMessage.getHeight();
        if ((scrollY + scrollViewMessageHeight + 50) >= messageHeight) {
            if (isScrollBottom) {//滑动到底部
                isScrollBottom = false;
                if (null != mOnMessageScrollChangeListener) {
                    mOnMessageScrollChangeListener.onScrollBottom();
                }
            }
        } else if (scrollY <= 50) {//滑动到顶部
            if (!isScrollTop) {
                isScrollTop = true;
                if (null != mOnMessageScrollChangeListener) {
                    mOnMessageScrollChangeListener.onScrollTop();
                }
            }
        } else if (scrollY > 50 && scrollY < messageHeight - scrollViewMessageHeight - 50) {
            isScrollTop = false;
            isScrollBottom = true;
        }
    }


    /************************************************** 标题 **************************************************/

    /**
     * 获取标题View
     *
     * @return TextView
     */
    public TextView getDialogTitleView() {
        return mTextViewTitle;
    }

    /**
     * 设置标题文字
     *
     * @param title
     */
    public void setDialogTitle(String title) {
        mTextViewTitle.setText(title);
    }

    /**
     * 设置标题是否可见
     *
     * @param visible
     */
    public void setDialogTitleVisible(boolean visible) {
        mTextViewTitle.setVisibility(visible ? View.VISIBLE : View.GONE);
        mViewSplitTitle.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    /************************************************** 内容 **************************************************/

    /**
     * 获取内容View
     *
     * @return TextView
     */
    public TextView getDialogMessageView() {
        return mTextViewMessage;
    }

    /**
     * 设置内容文字
     *
     * @param message
     */
    public void setDialogMessage(String message) {
        mTextViewMessage.setText(message);
    }

    /**
     * 设置内容滑动事件
     *
     * @param listener
     */
    public void setOnMessageScrollChangeListener(OnMessageScrollChangeListener listener) {
        this.mOnMessageScrollChangeListener = listener;
    }

    /************************************************** 取消 + 确定 **************************************************/

    /**
     * 获取取消View
     *
     * @return TextView
     */
    public TextView getDialogCancelView() {
        return mButtonCancel;
    }

    /**
     * 设置取消文字
     *
     * @param cancelText
     */
    public void setDialogCancelText(String cancelText) {
        mButtonCancel.setText(cancelText);
    }

    /**
     * 设置取消是否可见
     *
     * @param visible
     */
    public void setDialogCancelTextVisible(boolean visible) {
        mButtonCancel.setVisibility(visible ? View.VISIBLE : View.GONE);
        mViewSplitBottom.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    /**
     * 设置取消点击事件
     *
     * @param listener
     */
    public void setOnCancelClickListener(OnCancelClickListener listener) {
        this.mOnCancelClickListener = listener;
    }

    /**
     * 获取确认View
     *
     * @return TextView
     */
    public TextView getDialogConfirmView() {
        return mButtonConfirm;
    }

    /**
     * 设置确认文字
     *
     * @param confirmText
     */
    public void setDialogConfirmText(String confirmText) {
        mButtonConfirm.setText(confirmText);
    }

    /**
     * 设置确认是否可见
     *
     * @param visible
     */
    public void setDialogConfirmTextVisible(boolean visible) {
        mButtonConfirm.setVisibility(visible ? View.VISIBLE : View.GONE);
        mViewSplitBottom.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    /**
     * 设置确认点击事件
     *
     * @param listener
     */
    public void setOnConfirmClickListener(OnConfirmClickListener listener) {
        this.mOnConfirmClickListener = listener;
    }

    /************************************************** 分割线 **************************************************/

    /**
     * 获取标题分割线View
     *
     * @return View
     */
    public View getDialogTitleSplitLine() {
        return mViewSplitTitle;
    }

    /**
     * 设置标题分割线是否可见
     *
     * @param visible
     */
    public void setDialogTitleSplitLineVisible(boolean visible) {
        mViewSplitTitle.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
    }

    /**
     * 获取内容分割线View
     *
     * @return View
     */
    public View getDialogMessageSplitLine() {
        return mViewSplitMessage;
    }

    /**
     * 设置内容分割线是否可见
     *
     * @param visible
     */
    public void setDialogMessageSplitLineVisible(boolean visible) {
        mViewSplitMessage.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
    }

    /**
     * 获取底部按钮分割线View
     *
     * @return View
     */
    public View getDialogBottomSplitLine() {
        return mViewSplitBottom;
    }

    /**
     * 设置底部按钮分割线是否可见
     *
     * @param visible
     */
    public void setDialogBottomSplitLineVisible(boolean visible) {
        mViewSplitBottom.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
    }

    /************************************************** 点击事件回调 **************************************************/


    public interface OnConfirmClickListener {
        void onConfirmClick();
    }

    public interface OnCancelClickListener {
        void onCancelClick();
    }

    public interface OnMessageScrollChangeListener {
        void onScrollTop();

        void onScrollBottom();
    }

    public static class EasyToUse {
        /*
         * 简单使用：
        YDSAlertDialog alertDialog = new YDSAlertDialog(this, 0.85f, 0f);
        alertDialog.show();
        alertDialog.setDialogTitle("通知");
        alertDialog.setDialogMessage("百年风雨兼程，百年波澜壮阔。");
        TextView dialogTitleView = alertDialog.getDialogTitleView();
        dialogTitleView.setTextColor(Color.parseColor("#ff0000"));
        alertDialog.setOnMessageScrollChangeListener(new YDSAlertDialog.OnMessageScrollChangeListener() {
            @Override
            public void onScrollTop() {
            }

            @Override
            public void onScrollBottom() {
            }
        });

        alertDialog.setOnConfirmClickListener(new YDSAlertDialog.OnConfirmClickListener() {
            @Override
            public void onConfirmClick() {
            }
        });

        alertDialog.setOnCancelClickListener(new YDSAlertDialog.OnCancelClickListener() {
            @Override
            public void onCancelClick() {
            }
        });
         */
    }
}

